package io.mvvm.netty.codec;

import io.mvvm.netty.codec.entity.LoginRequestMessage;
import io.mvvm.netty.codec.entity.Message;
import io.mvvm.netty.codec.handler.MessageCodec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: Netty
 * @description:
 * @author: 潘
 * @create: 2021-05-16 22:16
 **/
public class CodecTest {

    public static void main(String[] args) throws Exception {
        EmbeddedChannel channel = new EmbeddedChannel(
                // 最大长度，偏移量，存储内容长度的长度，补偿几个字节到长度位置，去除多少字节内容
                new LengthFieldBasedFrameDecoder(1024, 12, 4, 0, 0),
                new LoggingHandler(LogLevel.INFO),
                new MessageCodec()
        );

        // encoder
        Message message = new LoginRequestMessage("root", "123456", "zhangsan");
        channel.writeOutbound(message);

        // decoder
        Message message1 = new LoginRequestMessage("root", "123456", "zhangsan");
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null, message1, buf);

        // 入站
        channel.writeInbound(buf);
    }
}
